import { HttpError } from '@/extend/express'
import { body, matchedData, validate } from '@/extend/express-validator'
import { genJWT, privateDecrypt, privateKey } from '@/utils'
import express from 'express'
import 'express-async-errors'




export const login = express.Router()
    .post('/login',
        express.json(),
        body(['userName', 'passwd']).trim().notEmpty().withMessage('用户名不能为空'),
        validate(),
        async (req, res) => {
            req.log.info('1234567890')
            const data = matchedData(req)
            if (data.userName !== 'admin') {
                throw new HttpError({ code: 10000, msg: 'username or passwd error!' })
            }

            const passwd = privateDecrypt(data.passwd)
            const token = await genJWT({ id: 123456 }, 60 * 60)
            res.json({ token })
        })
    .post('/logout',
        (req, res) => {
            res.status(200).send()
        }
    )


